package com.example.ceramics.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ceramics.R;
import com.example.ceramics.activity.DetailActivity;
import com.example.ceramics.database.dao.CartDaoImpl;
import com.example.ceramics.database.dao.ICartDao;
import com.example.ceramics.entity.CartEntity;
import com.example.ceramics.entity.GoodsEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<CartEntity> datas;
    private List<CartEntity> selectedGoods = new ArrayList<>();
    private double totalPrice;

    private OnItemClickListener mOnItemClickListener;
    private OnItemChooseListener mOnItemChooseListener;
    private OnItemDeleteListener mOnItemDeleteListener;
    private OnItemUpdateNumListener mOnItemUpdateNumListener;

    private ICartDao cartDao;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemChooseListener(OnItemChooseListener onItemChooseListener) {
        mOnItemChooseListener = onItemChooseListener;
    }

    public void setOnItemDeleteListener(OnItemDeleteListener onItemDeleteListener) {
        mOnItemDeleteListener = onItemDeleteListener;
    }

    public void setOnItemUpdateNumListener(OnItemUpdateNumListener onItemUpdateNumListener) {
        mOnItemUpdateNumListener = onItemUpdateNumListener;
    }

    public List<CartEntity> getDatas() {
        return datas;
    }

    public void setDatas(List<CartEntity> datas) {
        this.datas = datas;
    }

    public List<CartEntity> getSelectedGoods() {
        return selectedGoods;
    }

    public void setSelectedGoods(List<CartEntity> selectedGoods) {
        this.selectedGoods = selectedGoods;
    }

    public CartAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public CartAdapter(Context mContext, List<CartEntity> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_cart_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    //给控件对象赋值，给ViewHoleder绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        cartDao = new CartDaoImpl(mContext);
        ViewHolder viewHolder = (ViewHolder) holder;
        CartEntity cartEntity = datas.get(position);
        GoodsEntity goodsEntity = cartEntity.getGoodsEntity();
        viewHolder.tvName.setText(goodsEntity.getGoodsName());
        viewHolder.tvPrice.setText("￥" + goodsEntity.getGoodsPrice());
        viewHolder.tvType.setText(goodsEntity.getGoodsType());
        viewHolder.ivImg.setImageResource(goodsEntity.getGoodsImg());
//        viewHolder.cb.setChecked(goodsEntity.getCartSelected());
        viewHolder.cb.setChecked(false);
        viewHolder.tvNum.setText("" + cartEntity.getNum());
        viewHolder.goodsEntity = goodsEntity;

        viewHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(viewHolder.goodsEntity);
            }
        });
        viewHolder.tvPrice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(viewHolder.goodsEntity);
            }
        });
        viewHolder.tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(viewHolder.goodsEntity);
            }
        });
        viewHolder.ivImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(viewHolder.goodsEntity);
            }
        });

        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    goodsEntity.setCartSelected(true);
                    if (!selectedGoods.contains(cartEntity))
                        selectedGoods.add(cartEntity);
                } else {
                    goodsEntity.setCartSelected(false);
                    if (selectedGoods.contains(cartEntity))
                        selectedGoods.remove(cartEntity);
                }

                mOnItemChooseListener.onItemChoose(getTotalPrice());
            }
        });

        viewHolder.btnSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartDao.updateNum(goodsEntity.getGoodsId(), cartEntity.getAccount(), -1, goodsEntity.getGoodsPrice());
                cartEntity.setNum(cartEntity.getNum() - 1);
                cartEntity.setPrice(cartEntity.getPrice() - goodsEntity.getGoodsPrice());
                viewHolder.tvNum.setText("" + cartEntity.getNum());
                mOnItemUpdateNumListener.onItemUpdateNum(getTotalPrice());
            }
        });
        viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cartDao.updateNum(goodsEntity.getGoodsId(), cartEntity.getAccount(), 1, goodsEntity.getGoodsPrice());
                cartEntity.setNum(cartEntity.getNum() + 1);
                cartEntity.setPrice(cartEntity.getPrice() + goodsEntity.getGoodsPrice());
                viewHolder.tvNum.setText(cartEntity.getNum() + "");
                mOnItemUpdateNumListener.onItemUpdateNum(getTotalPrice());
            }
        });

//        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (viewHolder.cb.isChecked()) {
////                    goodsEntity.setCartSelected(false);
//                    viewHolder.cb.setChecked(false);
////                    if (selectedGoods.contains(goodsEntity))
////                        selectedGoods.remove(goodsEntity);
//                }
//                if (datas.contains(goodsEntity)) {
//                    datas.remove(goodsEntity);
//                    notifyDataSetChanged();
//                }
//
//                mOnItemDeleteListener.onItemDelete(getTotalPrice());
//            }
//        });
    }

    @Override
    public int getItemCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size();
        }
        return 0;
    }

    //绑定控件，得到控件对象
    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvPrice;
        private TextView tvSignature;
        private TextView tvType;
        private ImageView ivImg;
        private GoodsEntity goodsEntity;
        private CheckBox cb;
        private TextView tvNum;
        private Button btnSub;
        private Button btnAdd;
//        private ImageView ivDelete;

        public ViewHolder(@NonNull View view) {
            super(view);
            tvName = view.findViewById(R.id.tv_name);
            tvPrice = view.findViewById(R.id.tv_price);
//            tvSignature = view.findViewById(R.id.tv_name);
            tvType = view.findViewById(R.id.tv_type);
            ivImg = view.findViewById(R.id.iv_img);
            cb = view.findViewById(R.id.cb);
            tvNum = view.findViewById(R.id.tv_num);
            btnSub = view.findViewById(R.id.btn_sub);
            btnAdd = view.findViewById(R.id.btn_add);
//            ivDelete = view.findViewById(R.id.iv_delete);

//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mOnItemClickListener.onItemClick(goodsEntity);
//                }
//            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Serializable obj);
    }

    public interface OnItemChooseListener {
        void onItemChoose(double totalPrice);
    }

    public interface OnItemDeleteListener {
        void onItemDelete(double totalPrice);
    }

    public interface OnItemUpdateNumListener {
        void onItemUpdateNum(double totalPrice);
    }

    public double getTotalPrice() {
        totalPrice = 0;
        for (CartEntity cartEntity : selectedGoods) {
            totalPrice += cartEntity.getPrice();
        }
        return totalPrice;
    }

    public boolean delete() {
//        datas.removeAll(selectedGoods);
//        selectedGoods.clear();
//        notifyDataSetChanged();

        if (selectedGoods == null || selectedGoods.size() == 0) {
            return false;
        }
        cartDao.deleteCart(selectedGoods);
        selectedGoods.clear();
        return true;
    }
}
